//
//  HJActionSheet.m
//  Molian
//
//  Created by 胡津 on 15/3/28.
//  Copyright (c) 2015年 xiaocan. All rights reserved.
//

#import "HJActionSheet.h"

#define duration 0.55

#define sideViewDamping 0.87
#define sideViewVelocity 10

#define centerViewDamping 1.0
#define centerViewVelocity 8

@interface HJActionSheet()
@property(nonatomic,strong) UIView* sideHelperView;
@property(nonatomic,strong) UIView* centerHelperView;
@property(nonatomic,strong) CADisplayLink *displayLink;
@property(nonatomic,strong) UIView *contentView;
@property(nonatomic,strong) UIView*bgview;
@property BOOL shown;
@property int counter;

@end

@implementation HJActionSheet
@synthesize Delegate;



- (id) initWithTitle:(NSString*)title Delegate:(id)delegate  destructiveButtonTitle:(NSString*)destructiveButtonTitle OtherButtonTitles:(NSArray *)otherButtonTitles
{
    CGFloat height = otherButtonTitles.count*60+40;
    
    if (destructiveButtonTitle) {
        height +=70;
    }
    
    CGRect screenRect = [[UIScreen mainScreen] bounds];
    CGFloat screenWidth = CGRectGetWidth(screenRect);
    CGFloat screenHeight = CGRectGetHeight(screenRect);
    
    self=[super initWithFrame:CGRectMake(0, screenHeight-height, screenWidth, height)];
    if(self){
        
        self.Delegate =delegate;
        
        self.counter = 0;
        
        _bgview=[[UIView alloc]initWithFrame:[[UIApplication sharedApplication] keyWindow].bounds];
        _bgview.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.7];
        [[[UIApplication sharedApplication] keyWindow] addSubview:_bgview];
        [_bgview addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hide)]];


        
        self.contentView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, screenWidth, height)];
        self.contentView.transform = CGAffineTransformMakeTranslation(0, height);
        [self addSubview:self.contentView];
        
        self.sideHelperView = [[UIView alloc] initWithFrame:CGRectMake(0, height, 0, 0)];
        self.sideHelperView.backgroundColor=[UIColor blackColor];
        [self addSubview:self.sideHelperView];
        
        
        self.centerHelperView = [[UIView alloc] initWithFrame:CGRectMake(screenWidth/2, height, 0, 0)];
        self.centerHelperView.backgroundColor=[UIColor blackColor];
        [self addSubview:self.centerHelperView];
        
        self.backgroundColor=[UIColor clearColor];
        
        if (title) {
            UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(20, 10, DeviceWidth-40, 20)];
            label.textAlignment = NSTextAlignmentCenter;
            label.textColor = [UIColor whiteColor];
            label.font = [UIFont fontWithName:FontName size:15];
            label.text = title;
            [self addView:label];
        }
        
        if (destructiveButtonTitle) {
            UIButton * option1 = [self produceButtonWithTitle:destructiveButtonTitle];
            option1.frame=CGRectMake(15, self.bounds.size.height-56, DeviceWidth-30, 46);
            option1.tag = otherButtonTitles.count;
            [option1.layer setBorderColor:APPCOLOR(1).CGColor];
            [option1 setTitleColor:APPCOLOR(1) forState:UIControlStateNormal];
            [option1.layer setBorderWidth:1];
            [option1.layer setCornerRadius:CGRectGetHeight([option1 bounds]) / 2];
            option1.layer.masksToBounds = YES;//开启圆角

            [option1 addTarget:self action:@selector(optoon:) forControlEvents:UIControlEventTouchUpInside];
            [self addView:option1];
        }
        
        for (int i=0; i < otherButtonTitles.count; i++) {
            UIButton * option1 = [self produceButtonWithTitle:otherButtonTitles[i]];
            option1.tag = i;
            [option1 setTitleColor:RGBA(102, 102, 102, 1) forState:UIControlStateNormal];
            option1.frame=CGRectMake(15, 60*i+40, DeviceWidth-30, 46);
            [option1.layer setBorderColor:RGBA(204, 204, 204, 1).CGColor];
            [option1.layer setBorderWidth:1];
            [option1.layer setCornerRadius:CGRectGetHeight([option1 bounds]) / 2];
            option1.layer.masksToBounds = YES;//开启圆角
            [option1 addTarget:self action:@selector(optoon:) forControlEvents:UIControlEventTouchUpInside];
            [self addView:option1];
        }
        
    }
    return self;

    
}

-(void)optoon:(UIButton*)sender 
{
    [self hide];
    [self hjActionSheet:sender clickedButtonAtIndex:sender.tag];
}

-(void)hjActionSheet:(UIButton*)sender clickedButtonAtIndex:(NSInteger)buttonIndex
{
    [Delegate hjActionSheet:sender clickedButtonAtIndex:buttonIndex];

}

-(UIButton *) produceButtonWithTitle:(NSString*) title
{
    UIButton * button =[UIButton buttonWithType:UIButtonTypeCustom];
    button.backgroundColor= [UIColor whiteColor];
    button.layer.cornerRadius=23;
    button.titleLabel.textAlignment = NSTextAlignmentCenter;
    button.titleLabel.font = [UIFont fontWithName:FontName size:16];
    [button setTitle:title forState:UIControlStateNormal];
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    return button;
}

-(void) addView:(UIView *)view
{
    [self.contentView addSubview:view];
}

-(void) toggle
{
    if(self.shown){
        [self hide];
    }else{
        [self show];
    }
}



-(void) show
{
    if(self.counter!=0){
        return;
    }
    [[[UIApplication sharedApplication] keyWindow] addSubview:self];
    self.shown=YES;
    [self start];
    [self animateSideHelperViewToPoint:CGPointMake(self.sideHelperView.center.x, 0)];
    [self animateCenterHelperViewToPoint: CGPointMake(self.centerHelperView.center.x, 0)];
    [self animateContentViewToHeight:0];
    
}

-(void) hide
{
    if(self.counter!=0){
        return;
    }
    self.shown=NO;
    [self start];
    CGFloat height = CGRectGetHeight(self.bounds);
    [Utils Hidden:_bgview Bool:NO];

    [self animateSideHelperViewToPoint:CGPointMake(self.sideHelperView.center.x, height)];
    [self animateCenterHelperViewToPoint: CGPointMake(self.centerHelperView.center.x, height)];
    [self animateContentViewToHeight:height];
    
}

-(void) animateSideHelperViewToPoint:(CGPoint) point
{
    
    [UIView animateWithDuration:duration delay:0 usingSpringWithDamping:sideViewDamping initialSpringVelocity:sideViewVelocity options:0 animations:^{
        self.sideHelperView.center = point;
    } completion:^(BOOL finished) {
        [self complete];
        
    }];
}


-(void) animateCenterHelperViewToPoint:(CGPoint) point
{
    
    [UIView animateWithDuration:duration delay:0 usingSpringWithDamping:centerViewDamping initialSpringVelocity:centerViewVelocity options:0 animations:^{
        self.centerHelperView.center = point;
        
    } completion:^(BOOL finished) {
        [self complete];
    }];
}

-(void) animateContentViewToHeight:(CGFloat) height
{
    [UIView animateWithDuration:duration delay:0 usingSpringWithDamping:centerViewDamping initialSpringVelocity:centerViewVelocity options:0 animations:^{
        self.contentView.transform = CGAffineTransformMakeTranslation(0, height);
    } completion:^(BOOL finished) {
    }];
}




-(void) tick:(CADisplayLink*) displayLink
{
    //NSLog(@"%@", NSStringFromCGPoint(self.centerHelperView.center));
    [self  setNeedsDisplay];
}

-(void) start
{
    if (self.displayLink == nil) {
        self.displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(tick:)];
        
        [self.displayLink addToRunLoop:[NSRunLoop currentRunLoop]
                               forMode:NSDefaultRunLoopMode];
        self.counter=2;
    }
}

-(void) complete
{
    self.counter--;
    if(self.counter==0){
        [self.displayLink invalidate];
        self.displayLink = nil;
        if(!self.shown){
            [self removeFromSuperview];
        }
    }
}
- (void)drawRect:(CGRect)rect
{
    if(self.counter==0){
        return;
    }
    CALayer* sideLayer=self.sideHelperView.layer.presentationLayer;
    CGPoint sidePoint=sideLayer.frame.origin;
    
    CALayer* centerLayer =self.centerHelperView.layer.presentationLayer;
    CGPoint centerPoint=centerLayer.frame.origin;
    
    UIBezierPath* path = [UIBezierPath bezierPath];
    
    [[UIColor whiteColor] setFill];
    
    [path moveToPoint:sidePoint];
    [path addQuadCurveToPoint:CGPointMake(DeviceWidth, sidePoint.y) controlPoint:centerPoint];
    [path addLineToPoint:CGPointMake(DeviceWidth, CGRectGetHeight(self.bounds))];
    [path addLineToPoint:CGPointMake(0, CGRectGetHeight(self.bounds))];
    [path closePath];
    
    [path fill];
}



@end
